package com.example.myapplication.lifecycle;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;

public class MyTestView extends View implements LifecycleOwner {
    private LifecycleRegistry lifecycleRegistry;

    public MyTestView(Context context) {
        this(context, null);
    }

    public MyTestView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyTestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        lifecycleRegistry = new LifecycleRegistry(this);
        lifecycleRegistry.setCurrentState(Lifecycle.State.CREATED);
    }

    public void destroy() {
        lifecycleRegistry.setCurrentState(Lifecycle.State.DESTROYED);
    }


    @NonNull
    @Override
    public Lifecycle getLifecycle() {
        return lifecycleRegistry;
    }
}
